package org.example.day07;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class StuManagerList {
    private List<Student> stus = new ArrayList<>();
    Scanner input = new Scanner(System.in);
    //根据id查询学生方法
    public Student getStuById(int id){
        Student student = null;
        for (Student stu : stus){
            if (stu.getId() == id){
                student = stu;
                break;
            }
        }
        return student;
    }
//添加一位学生
    public void add(){
        Student student = new Student();
        System.out.println("请输入学生的ID:");
        student.setId(input.nextInt());
        System.out.println("请输入学生的分数:");
        student.setScore(input.nextInt());
        if (getStuById(student.getId()) == null){

            this.stus.add(student);
            System.out.println("学生分数添加成功");
        }else System.out.println("该学生信息已存在");
    }
    //查找学生分数
    public void showStuScore(){
        System.out.println("请输入学生的ID:");
        int id = input.nextInt();
        Student student = getStuById(id);
        if (student != null){
            System.out.println("该同学的分数是: "+student.getScore());
        }else System.out.println("该ID不存在");
    }
    //删除学生
    public void deleteStuById(){
        System.out.println("请输入学生的ID:");
        int id = input.nextInt();
        Student student = getStuById(id);
        if (student != null){
            this.stus.remove(student);
            System.out.println("学生删除成功");
        }else System.out.println("该ID不存在");
    }
    //修改学生分数
    public void editStuById(){
        System.out.println("请输入修改学生的ID:");
        int id = input.nextInt();
        Student student = getStuById(id);
        if (student != null){
            System.out.println("请输入修改后的分数");
            student.setScore(input.nextInt());
        }else System.out.println("该ID不存在");
    }
    public void menu(){
        while (true){
            System.out.println("\n*****************************");
            System.out.println("1 添加学生分数");
            System.out.println("2 查找学生分数");
            System.out.println("3 修改学生分数");
            System.out.println("4 删除学生分数");
            System.out.println("5 所有学生分数信息");
            System.out.println("6 退出系统");
            System.out.println("*****************************\n");
            System.out.println("请选择: ");
            int num = input.nextInt();
            switch (num){
                case 1:
                    add();
                    break;
                case 2:
                    showStuScore();
                    break;
                case 3:
                        editStuById();
                    break;
                case 4:
                    deleteStuById();
                    break;
                case 5:
                    showStus();
                    break;
                case 6:
                    System.out.println("退出系统");
                    return;
                default:
                    System.out.println("菜单选择有误!!!请重新选择");
            }
        }
    }

    private void showStus() {
        System.out.println("Id\t分数");

        for (Student stu : stus){
            System.out.println(stu.getId()+"\t"+stu.getScore());
    }
    }
}
